package com.websystique.springsecurity.controller;

import com.websystique.springsecurity.dao.SysMenuDao;
import com.websystique.springsecurity.model.SysMenu;
import com.websystique.springsecurity.model.User;
import com.websystique.springsecurity.model.UserProfile;
import com.websystique.springsecurity.model.VerificationMail;
import com.websystique.springsecurity.service.MailService;
import com.websystique.springsecurity.service.SysMenuService;
import com.websystique.springsecurity.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
public class HelloWorldController {

	@Autowired
	private UserService userService;
	@Autowired
	private SysMenuDao sysMenuDao;
	@Autowired
	private SysMenuService sysMenuService;
	@Autowired
	private MailService mailService;

	//注册界面
	@RequestMapping(value = {  "/register" }, method = RequestMethod.GET)
	public String registerGet(HttpServletRequest request, Model model) {
		String path=request.getContextPath();
		System.out.println("path   "+path);
		model.addAttribute("registerUrl", path+"/registerPost");
		model.addAttribute("loginurl", path+"/login");
		return "register";
	}
	//注册提交
	@RequestMapping(value = {"/registerPost"}, method = RequestMethod.GET)
	//String firstName,String lastName,String email,String ssoId,String password
	public String registerPost(@ModelAttribute User user,Model model) {
		user.setState("not");
		Set<UserProfile> set=new HashSet<>();
		UserProfile userProfile=new UserProfile();
		userProfile.setId(1);
		userProfile.setType("USER");
		set.add(userProfile);
		user.setUserProfiles(set);

		boolean b=true;//userService.save(user);
		if(b){model.addAttribute("isOk","true");}else{model.addAttribute("isOk","false");}
		return "register";
	}

	// 主页
	@RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET)
	public String homePage(ModelMap model) {
		VerificationMail mail=new VerificationMail();
		mail.setEmail("2861499942@qq.com");
		mail.setUsername(" moi");
		mail.setPassword("123456");
//		boolean b=mailService.sendVerificationMail(mail);
		System.out.println("++++++++++++++++++1212");
		model.addAttribute("user", getPrincipal());

		return "welcome";
	}

	@RequestMapping(value = "/admin", method = RequestMethod.GET)
	public String adminPage(ModelMap model) {
		model.addAttribute("user", getPrincipal());
		return "admin";
	}
	
	@RequestMapping(value = "/db", method = RequestMethod.GET)
	public String dbaPage(ModelMap model) {
		model.addAttribute("user", getPrincipal());
		return "dba";
	}

	@RequestMapping(value = "/Access_Denied", method = RequestMethod.GET)
	public String accessDeniedPage(ModelMap model) {
		model.addAttribute("user", getPrincipal());
		return "accessDenied";
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String loginPage() {
		return "login";
	}



	@RequestMapping(value="/logout", method = RequestMethod.GET)
	public String logoutPage (HttpServletRequest request, HttpServletResponse response) {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		if (auth != null){    
			new SecurityContextLogoutHandler().logout(request, response, auth);
		}
		return "redirect:/home";
//		return "redirect:/login?logout";//You can redirect wherever you want, but generally it's a good idea to show login screen again.
	}

	@RequestMapping(value = "/newUser", method = RequestMethod.GET)
	@ResponseBody
	public String newRegistration(ModelMap model) {
		User user = new User();
		user.setSsoId("sbsb");
		user.setPassword("sbsb");
		user.setEmail("112");
		user.setFirstName("sb");
		user.setLastName("sb");
		user.setState("Active");
		model.addAttribute("user", user);
		userService.save(user);
		return "newuser";
	}



	private String getPrincipal(){
		String userName = null;
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (principal instanceof UserDetails) {
			userName = ((UserDetails)principal).getUsername();
		} else {
			userName = principal.toString();
		}
		return userName;
	}

}